import scrapy
from scrapy import Request
import json
import datetime
from scrapyspider.items import Contest


class NewcoderSpider(scrapy.Spider):

    name = 'contest_newcoder'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36',
    }

    def start_requests(self):
        url = 'https://www.nowcoder.com/acm/contest/vip-index'
        yield Request(url, headers=self.headers)


    def parse(self, response):
        # print('res:',response.body())
        contests = response.xpath('//div[@class="platform-item js-item"]')

        def gettime(t):
            t = float(t) / 1000
            return datetime.datetime.fromtimestamp(t).strftime("%Y-%m-%d %H:%M:%S")

        for con in contests:
            item = Contest()
            jsondata = json.loads(con.xpath('@data-json').extract()[0])
            item['contest_name'] = jsondata['contestName']
            item['contest_url'] = 'https://www.nowcoder.com/acm/contest/'+str(jsondata['contestId'])
            item['signup_start_time'] = gettime(jsondata['contestSignUpStartTime'])
            item['signup_end_time'] = gettime(jsondata['contestSignUpEndTime'])
            item['contest_start_time'] = gettime(jsondata['contestStartTime'])
            item['contest_end_time'] = gettime(jsondata['contestEndTime'])
            item['sponsor_id'] = 1 #jsondata['settingInfo']['organizerName']
            item['hot_degree'] = jsondata['signUpCount']
            # item['status'] = con.xpath('span[contains(@class,"match-status")]/text()').extract()
            # print(title,url,signup_start_time,signup_end_time,contest_start_time,contest_end_time,publisher,hot_degree,status)
            yield item

